TOOL.Category="Construction"
TOOL.Name="#Harvester"
TOOL.Command=nil
TOOL.ConfigName=""

TOOL.ClientConVar['suck'] = "0"
TOOL.ClientConVar['spit'] = "1"

cleanup.Register("harvesters")

if ( CLIENT ) then
	language.Add( 'Tool_harvester_name', 'Wraith Harvester' )
	language.Add( 'Tool_harvester_desc', 'From Stargate Atlantis' )
	language.Add( 'Tool_harvester_0', 'One button sucks stuff up, the other one spits it out' )

	language.Add( 'Undone_harvesters', 'Harvester undone' )
	language.Add( 'Cleanup_harvesters', 'Harvesters' )
	language.Add( 'Cleaned_harvesters', 'Removed all Harvesters' )
	language.Add( 'SBoxLimit_harvesters', 'Maximum Harvesters reached' )
end

function TOOL:LeftClick( trace )
	if trace.Entity && (trace.Entity:IsPlayer() ) then return false end
	if(CLIENT) then	return true	end
	if(!SERVER) then return false end
	if ( !self:GetSWEP():CheckLimit( "harvesters" ) ) then return false end
	local ply = self:GetOwner()
	local Pos = trace.HitPos
	local Ang = trace.HitNormal:Angle()
	Ang.pitch = Ang.pitch + 90
	local onkey	= self:GetClientNumber( 'suck' )
	local offkey	= self:GetClientNumber( 'spit' )
	ent = MakeHarvester( ply, Ang, Pos, onkey, offkey)
	ent:SetPos( trace.HitPos - trace.HitNormal * ent:OBBMins().z)
	if ( trace.Entity:IsValid() ) then
		const = constraint.Weld(ent, trace.Entity,0, trace.PhysicsBone, 0, systemmanager )
		trace.Entity:DeleteOnRemove( ent )
	end
	undo.Create('harvester')
		undo.AddEntity(ent)
		undo.AddEntity(const)
		undo.AddEntity(nocollide)
		undo.SetPlayer(ply)
	undo.Finish()
	ply:AddCleanup( 'harvesters', const )
	ply:AddCleanup( 'harvesters', nocollide )
	return true
end

if SERVER then 
    CreateConVar('sbox_maxharvesters',1)
	function MakeHarvester( ply, Ang, Pos, suck,spit)
		if ( !ply:CheckLimit( "harvesters" ) ) then return nil end
		local ent = ents.Create( 'wraith_harvester' )
			if !ent:IsValid() then return false end
			ent:SetAngles(Ang)
			ent:SetPos(Pos)
		ent:Spawn()
		ent.Scale=size
		ent:SetVar("Owner",ply)
		numpad.OnDown(ply, suck, 'SuckOn', ent)
		numpad.OnUp(ply, suck, 'SuckOff', ent)
		numpad.OnUp(ply, spit, 'Spit', ent)
		local rtable = {
			suck=suck,
			spit=spit
			}
		table.Merge(ent:GetTable(), rtable )
		ply:AddCount('harvesters', ent)
		return ent
	end
	
	duplicator.RegisterEntityClass( "wraith_harvester", MakeHarvester, "Ang", "Pos", "suck", "spit" )
	
	local function SuckOn( ply, ent)
		if (!ent:IsValid()) then return false end
		ent:Fire("on","",0)
	end
	local function SuckOff( ply, ent)
		if (!ent:IsValid()) then return false end
		ent:Fire("off","",0)
	end
	local function Spit( ply, ent)
		if (!ent:IsValid()) then return false end
		ent:Fire("spit","",0)
	end
	numpad.Register("SuckOn",SuckOn)
	numpad.Register("SuckOff",SuckOff)
	numpad.Register("Spit",Spit)
end

function TOOL.BuildCPanel( cp )
	cp:AddControl( 'Header', { Text = '#Tool_harvester_name', Description	= '#Tool_harvester_desc' }  )
	cp:AddControl('Numpad', { 
		ButtonSize = '22', 
		Label = 'Suck:', 
		Command = 'harvester_suck',
		} )	
	cp:AddControl('Numpad', { 
		ButtonSize = '22', 
		Label = 'Spit:', 
		Command = 'harvester_spit',
		} )			
end
